package com.yj.suncity.publish;

import java.util.List;

import com.yj.suncity.R;
import com.yj.suncity.utis.Utils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class PublishGalleryAdapter extends BaseAdapter {

	private List<String> paths;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;
	private Context context;
	private int mWidth;

	public PublishGalleryAdapter(Context context, List<String> paths) {
		super();
		this.paths = paths;
		this.context = context;
		mWidth = context.getResources().getDisplayMetrics().widthPixels;
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.android_layout_bg)
				.showImageForEmptyUri(R.drawable.android_layout_bg)
				.showImageOnFail(R.drawable.android_layout_bg)
				.cacheInMemory(true).cacheOnDisc(true)
				//.displayer(new RoundedBitmapDisplayer(10))// 设置成圆角图片
				.bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
				.build();
	}

	@Override
	public int getCount() {
		return paths.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adaper_publish_gallery_img, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.gallery_img);
			holder.imageView.setLayoutParams(new RelativeLayout.LayoutParams(
					(mWidth - (Utils.dipTopx(context, 15))) / 3,
					(mWidth - (Utils.dipTopx(context, 15))) / 3));
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (position==0) {
			holder.imageView.setImageResource(R.drawable.btn_publish_add);
		}else{
			imageLoader.displayImage("file://"+paths.get(position-1), holder.imageView, options);
		}
		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
	}

}
